package cn.gson.springboot.model.service;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;

import cn.gson.springboot.model.dao.RoleDao;
import cn.gson.springboot.model.dao.UserDao;
import cn.gson.springboot.model.mapper.RoleMapper;
import cn.gson.springboot.model.mapper.UserMapper;
import cn.gson.springboot.model.pojos.Role;
import cn.gson.springboot.model.pojos.User;
import cn.gson.springboot.model.vo.FunctionVo;
import cn.gson.springboot.model.vo.FunctionVoo;
import cn.gson.springboot.model.vo.Rolevo;

@Service
/*1*/
@Transactional(rollbackFor=Exception.class)
public class UserService {
	@Autowired
	private UserMapper um;
	
	@Autowired
	private UserDao udao;
	
	@Autowired
	private RoleDao rdao;
	
	@Autowired
	private RoleMapper rm;
	
	
	//dao类新增
	public void addUser(User u) {
		udao.save(u);
	}
 
	

	 //查询所有用户
	 public JSONArray findUsers(String user_name){
			return um.findUsers(user_name);
		}
	 
	 public JSONArray dengluquanxian(Integer userId){
			return um.dengluquanxian(userId);
		}
	 
	
	 //新增用户
	 public void adduser(Integer keshi,String name,String password,String sex,String card,String tel,String Address,String zhuangtai,Integer roleIds) {
	    	um.adduser(keshi, name, password, sex, card, tel, Address, zhuangtai);
	    	//查出用户id
	    	JSONArray selectlimituser = um.selectlimituser();
	    	Integer userId = selectlimituser.getJSONObject(0).getInteger("user_id");	   
	    	um.adduserrole(userId,roleIds);
	    	
	 }
	 //新增角色
	 public void addrole(String roleName) {
		 rm.addrole(roleName);
	 }
	 
	 //删除角色
     public void deleteRoleA(Integer roleId) {
    	 rm.deleteRoleA(roleId);
     }
	 
	//新增用户角色
		 public void adduser111(Integer userId,List<Integer>  ids) {
			 //找到用户
		    User user = udao.findById(userId).get();
		    //主控房集合
		    List<Role> role = user.getRoles();
		 	if(role == null) {
		 		role = new ArrayList<>();
			}
		 	//清空
			role.clear();
			//往集合中添加功能的对象
			List<Role> allFuns = (List<Role>) rdao.findAllById(ids);		
						/* for (int i = 0; i < allFuns.size(); i++) {						
							    int q=0;
								int bb=allFuns.get(i).getRoleId();
								System.out.println("传过来新增的角色ID"+bb);
								JSONArray allrole = um.seclctAllrole(userId);						
								System.out.println("当前用户的角色"+allrole);							
								for (int j = 0; j < allrole.size(); j++) {
									 int aa=allrole.getJSONObject(j).getInteger("role_id");	
									 um.deleteadduserrole(userId, aa);								
                                     System.err.println("删除"+userId+"用户的"+aa+"角色");
									 if(aa==bb){
										 	System.out.println("已有角色id:"+"————————————————"+aa);									 	
											System.out.println("新增的角色"+bb+"————————————————"+"用户"+userId);										
									 }								
									 if(q==0) {
											System.out.println("新增");
											System.err.println("新增的角色编号"+bb+"————————————————"+"用户编号"+userId);											
											um.adduserrole11(userId, bb);
									}
								}
						 }	*/
						role.addAll(allFuns);	
		  }				

		 
	//根据id查询
		public User findUsersbyid(Integer uid) {
			return um.findUsersbyid(uid);
			
		}
	 
	//修改在职状态
	public void updatestatre(Integer userId,String user_state) {
		um.updatestatre(userId,user_state);
	}
	
	//根据ID和原密码查询
	public User findUserspwd(Integer userId,String userPwd) {
		return um.findUserspwd(userId, userPwd);
	}
	
	//修改密码
	public void updatepwd(Integer userId,String userPwd) {
		um.updatepwd(userId,userPwd);
	}
	
	//查看详情
	 public JSONArray findUsersdetails(Integer uid) {
		 return um.findUsersdetails(uid);
	 }
	
	//查找谋个用户拥有的角色
	 public JSONArray selectAllrole(Integer uid) {
		 return um.seclctAllrole(uid);
	 }
	
	//查找谋个用户拥有的功能权限
		 public List<FunctionVoo> selectAllfunction(Integer userid) {
			 return um.seclctAllfunction(userid);
			 
			
		 }

	

		/* 删除用户角色*/
		 public void deleteuserRole(Integer userId) {
			um.deleteuserRole(userId);
		 }
		 
		//用户添加角色初始化
		 public List<Rolevo> grantList(Integer userId) {
				return um.userroleList(userId);				
			}

		
		

	//查询科室	 
	public JSONArray selectUserSectionAll() {
		return um.selectUserSectionAll();
	}
	public JSONArray selectzhsectionid(String sname) {
		return um.selectzhsectionid(sname);
	}
	
	//查询用户表中医生，用于初始化医生的下拉列表
	public JSONArray selectUserDoctorAll(Integer sid) {
		return um.selectUserDoctorAll(sid);
	}
	//护士
	public JSONArray selectUserNurseAll(Integer sid) {
		return um.selectUserNurseAll(sid);
	}
	//根据安排的时间来查询在上班的医生
	public JSONArray selectopsanarrdoctor(Date daytime,String starttime,String endtime,
			Integer sectionid) {
		return um.selectopsanarrdoctor(daytime, starttime, endtime, sectionid);
	}
	 //根据医生名字查id
	 public User selectByname(String uname) {
		 return um.selectByname(uname);
	 }
}
